package com.trkj.service.hjj;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.hjj.AffairtypeconfirmDAO;
import com.trkj.dao.hjj.HolidayDAO;
import com.trkj.dao.hjj.LeaveconfirmDAO;
import com.trkj.dao.hjj.WorkOvertimeToConfirm;
import com.trkj.pojo.Affaircategory;
import com.trkj.pojo.Affairtypeconfirm;
import com.trkj.pojo.Holiday;
import com.trkj.pojo.Leaveconfirm;
import com.trkj.pojo.StaffInformation;
import com.trkj.service.ServiceException;
import com.trkj.vo.AffaircatetypeVo;
import com.trkj.vo.LeaveVo;

@Service
@Transactional(rollbackFor=Exception.class)//事务回滚
public class ConfirmTransactionService {
	//请假确认
	@Autowired
	private LeaveconfirmDAO leaveconfirmDAO;
	
	//假期
	@Autowired
	private HolidayDAO holidayDAO;
	//总事务类型确认
	@Autowired
	private AffairtypeconfirmDAO affaortypeconfirmDAO;
	@Autowired
	private WorkOvertimeToConfirm workOvertimeToConfirm;
	/**
	 * 加班
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<AffaircatetypeVo> finadjiaban(int page,int rows)throws ServiceException{
		try {
			List<Object[]> list = workOvertimeToConfirm.findAllWorkOvertimeToConfirms(new PageRequest(page-1, rows));
			
			List<AffaircatetypeVo> listvo = new ArrayList<AffaircatetypeVo>();
			if(list!=null){
				for (Object[] listobj : list) {
					Affairtypeconfirm a = (Affairtypeconfirm) listobj[0];
					StaffInformation s = (StaffInformation) listobj[1];
					Affaircategory af =(Affaircategory) listobj[2];
					
					AffaircatetypeVo avo = new AffaircatetypeVo();
					
					avo.setAffairId(a.getAtcId());
					avo.setStainId(s.getStainId());
					avo.setStainName(s.getStainName());
					avo.setAffairName(af.getAffcaName());
					avo.setAffairRemark(a.getAtcReason());
					avo.setStartTime(a.getAtcStartdate());
					avo.setIsTime(a.getAtcFounddate());
					avo.setEndTime(a.getAtcEnddate());
					
					listvo.add(avo);
				}
			}
			return listvo;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 外勤确认
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<AffaircatetypeVo> findAllAffairtype(int page,int rows)throws ServiceException{
		try {
			List<Object[]> list = affaortypeconfirmDAO.findAllAffairtype(new PageRequest(page-1, rows));
			System.out.println(list);
			List<AffaircatetypeVo> listvo = new ArrayList<AffaircatetypeVo>();
			if(list!=null){
				for (Object[] listobj : list) {
					Affairtypeconfirm a = (Affairtypeconfirm) listobj[0];
					StaffInformation s = (StaffInformation) listobj[1];
					Affaircategory af =(Affaircategory) listobj[2];
					
					AffaircatetypeVo avo = new AffaircatetypeVo();
					
					avo.setAffairId(a.getAtcId());
					avo.setStainId(s.getStainId());
					avo.setStainName(s.getStainName());
					avo.setAffairName(af.getAffcaName());
					avo.setAffairRemark(a.getAtcReason());
					avo.setStartTime(a.getAtcStartdate());
					avo.setIsTime(a.getAtcFounddate());
					avo.setEndTime(a.getAtcEnddate());
					
					listvo.add(avo);
				}
			}
			return listvo;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 外勤确认总记录数
	 * @return
	 */
	public long ConfirmCount(){
		return affaortypeconfirmDAO.count();
	}
	/**
	 * 查询所有的请假确认
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<LeaveVo> findAllLeave(int page,int rows)throws ServiceException{
		try {
			List<Object[]> list = leaveconfirmDAO.findallleaveconf(new PageRequest(page-1, rows));	
			List<LeaveVo> listvo=new ArrayList<LeaveVo>();
				if(list!=null){
					for (Object[] obj : list) {
						Leaveconfirm l = (Leaveconfirm) obj[0];
						Holiday h = (Holiday) obj[2];
						StaffInformation s = (StaffInformation) obj[1];
						
						LeaveVo lvo = new LeaveVo();
						lvo.setLeaconId(l.getLeaconId());
						lvo.setLeaconEnddate(l.getLeaconEnddate());
						lvo.setLeaconFounddate(l.getLeaconFounddate());
						lvo.setLeaconPerson(l.getLeaconPerson());
						lvo.setLeaconReason(l.getLeaconReason());
						lvo.setLeaconStartdate(l.getLeaconStartdate());
						
						lvo.setHolidayName(h.getHolidayName());
						
						lvo.setStainId(s.getStainId());
						lvo.setStainName(s.getStainName());
						
						
						listvo.add(lvo);
					}
				}
				return listvo;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 请假确认总记录数
	 * @return
	 */
	public long leaveCount(){
		return leaveconfirmDAO.count();
	}
	/**
	 * 查询所有假种
	 * @return
	 */
	public List<Holiday> findAllHoliday(){
		return (List<Holiday>) holidayDAO.findAll();
	}

	
	
	public long jiabanCounts(){
		return workOvertimeToConfirm.count();
	}
}
